(function(window,document){
	"use strict"
	var $resultContainer;
	$(function(){
		$resultContainer=$("#resultContainer");
		$resultContainer.html("");
		show();
		$("#btn").click(usekey);
	});
	var show=function(){
		var url="/api/doc/list";
		$.get(url,function(resp){
			console.log(resp);
			
			$("#totalRows").html(resp.data.length);
			var data=resp.data;
			data = listree(data);
			//console.log(data);
			//找到所有的父节点
			painttree(data);
		});
	};
	var painttree = function(data) {
		$("#functionalityTableTree").fancytree({
			//source:data,
			extensions: ["table"],
			minExpandLevel:3,
			checkbox: true,
			table: {
				indentation: 20, // indent 20px per node level
				nodeColumnIdx: 1, // render the node title into the 2nd column
			//	checkboxColumnIdx: 0 render the checkboxes into the 1st column
			},
			source: data,
			renderColumns: function(event, data) {
				//console.log(data);
				//node对象
				var node = data.node;
				//传入的node模型
				var nodeData=node.data;
				//tr标签
				var tr=node.tr;
				var tds=tr.cells;
				//显示序号
				tds[0].textContent=node.getIndexHier();
				//第二列
				//tds[1].textContent=nodeData.title;
				//名字
				tds[2].innerHTML="<a href='check.html#"+nodeData.rows.id+"'>查看</a>";
				//url 
				tds[3].textContent=$$(getD(nodeData.rows.createTime));
				//remark
				tds[4].innerHTML=$$(nodeData.createName);
			}
		});
	}
	var getD=function(f){
		return (new Date(f).toLocaleDateString()+""+new Date(f).toLocaleTimeString());
	}
	var usekey=function(){
		var i=$("#key").val();
		var j=$("#state").val();
		var url="/api/system/user/list";
		$.get(url,{key:i,state:j},function(resp){
			$("#totalRows").html(resp.data.totalRows);
			$("#pages").html(resp.data.pageSize);
			$("#totalPages").html(resp.data.totalPages);
			if(resp.data.totalPages<=resp.data.pageNo){
				$("#pageno").html(resp.data.totalPages);
			}else{
				$("#pageno").html(resp.data.pageNo);
			}
		paintContent(resp.data.rows);
		});
	};
	
	var  listree=function (list) {
		//保存所有根节点
		var roots=[];
		//保存所有节点
		var nodeMap={};
		list.filter(function(node){
			node.key=node.id;
			node.title=node.name;				
			nodeMap[node.id]=node;
			//如果父节点id未定义或未null,则表示未跟节点
			if(node.parentId===null||typeof node.parentId==="undefined"){
				roots.push(node);
				return false;
			}
			return true;
		}).forEach(function(node){
			//找到父节点
			var parent=nodeMap[node.parentId];
			if(parent){
				(parent.children=parent.children||[]).push(node);
			}else{
				//未找到父节点
				console.error("节点"+node+"未找到父节点");
			}
		});
		return roots;
	};
	
})(window,document);
